﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;
using System.Net;
using System.Collections.Specialized;

static class WebSaver
{
    public static String saveUser(string username, string password, string email, int money, int experience)
    {
        WebClient client = new WebClient();
        NameValueCollection postData = new NameValueCollection();

        postData.Add("upload_mode", "user");
        postData.Add("username", username);
        postData.Add("password", password);
        postData.Add("email", email);
        postData.Add("money", money.ToString());
        postData.Add("experience", experience.ToString());
        postData.Add("version", jaysJourney_v01.Common.CONST.VERSION);

        byte[] responseArray;
        try
        {
            responseArray = client.UploadValues("http://www.best-designs.at/pro4/WebSaver.php", "POST", postData);
        }
        catch (WebException)
        {
            return null;
        }

        Debug.WriteLine(Encoding.ASCII.GetString(responseArray));
        return Encoding.ASCII.GetString(responseArray);
    }

    public static bool saveWorld(string worldname, string username, string setting, string size, string worldData, string bestTime)
    {
        WebClient client = new WebClient();
        NameValueCollection postData = new NameValueCollection();

        postData.Add("upload_mode", "world");
        postData.Add("world_name", worldname);
        postData.Add("user_name", username);
        postData.Add("setting", setting);
        postData.Add("size", size);
        postData.Add("world_data", worldData);
        postData.Add("best_time", bestTime);
        postData.Add("min_lvl", getMinLvl(worldData, setting).ToString());

        byte[] responseArray;
        try
        {
            responseArray = client.UploadValues("http://www.best-designs.at/pro4/WebSaver.php", "POST", postData);
        }
        catch (WebException)
        {
            return false;
        }
        return true;
    }

    public static bool userUpdate(string username, int money, int experience)
    {
        WebClient client = new WebClient();
        NameValueCollection postData = new NameValueCollection();

        postData.Add("upload_mode", "userUpdate");
        postData.Add("username", username);
        postData.Add("money", money.ToString());
        postData.Add("experience", experience.ToString());

        byte[] responseArray;
        try
        {
            responseArray = client.UploadValues("http://www.best-designs.at/pro4/WebSaver.php", "POST", postData);
        }
        catch (WebException)
        {
            return false;
        }
        return true;
    }

    public static bool worldUpdate(string worldname, int numStarts, int numRates, double curRating, int bestTime, string user_besttime, int avgTime)
    {
        WebClient client = new WebClient();
        NameValueCollection postData = new NameValueCollection();

        postData.Add("upload_mode", "worldUpdate");
        postData.Add("world_name", worldname);
        postData.Add("number_starts", numStarts.ToString());
        postData.Add("number_ratings", numRates.ToString());
        postData.Add("current_rating", curRating.ToString().Replace("," , "."));
        postData.Add("best_time", bestTime.ToString());
        postData.Add("user_besttime", user_besttime);
        postData.Add("avg_besttime", avgTime.ToString());

        byte[] responseArray;
        try
        {
            responseArray = client.UploadValues("http://www.best-designs.at/pro4/WebSaver.php", "POST", postData);
        }
        catch (WebException)
        {
            return false;
        }
        return true;
    }

    public static bool creatorUpdate(string username)
    {
        WebClient client = new WebClient();
        NameValueCollection postData = new NameValueCollection();

        postData.Add("upload_mode", "creatorUpdate");
        postData.Add("username", username);

        byte[] responseArray;
        try
        {
            responseArray = client.UploadValues("http://www.best-designs.at/pro4/WebSaver.php", "POST", postData);
        }
        catch (WebException)
        {
            return false;
        }
        return true;
    }

    public static bool creatorResetChanges(string username)
    {
        WebClient client = new WebClient();
        NameValueCollection postData = new NameValueCollection();

        postData.Add("upload_mode", "creatorReset");
        postData.Add("username", username);

        byte[] responseArray;
        try
        {
            responseArray = client.UploadValues("http://www.best-designs.at/pro4/WebSaver.php", "POST", postData);
        }
        catch (WebException)
        {
            return false;
        }
        return true;
    }

    public static bool updatePersonalBestTime(string username, string worldName, int personalBestTime)
    {
        WebClient client = new WebClient();
        NameValueCollection postData = new NameValueCollection();

        postData.Add("upload_mode", "updatePersonalBestTime");
        postData.Add("username", username);
        postData.Add("world_name", worldName);
        postData.Add("personal_besttime", personalBestTime.ToString());

        byte[] responseArray;
        try
        {
            responseArray = client.UploadValues("http://www.best-designs.at/pro4/WebSaver.php", "POST", postData);
        }
        catch (WebException)
        {
            return false;
        }
        return true;
    }

    public static bool createPersonalBestTime(string username, string worldName, int personalBestTime)
    {
        WebClient client = new WebClient();
        NameValueCollection postData = new NameValueCollection();

        postData.Add("upload_mode", "createPersonalBestTime");
        postData.Add("username", username);
        postData.Add("world_name", worldName);
        postData.Add("personal_besttime", personalBestTime.ToString());

        byte[] responseArray;
        try
        {
            responseArray = client.UploadValues("http://www.best-designs.at/pro4/WebSaver.php", "POST", postData);
        }
        catch (WebException)
        {
            return false;
        }
        return true;
    }

    private static int getMinLvl(String data,String setting)
    {
        int minLvl = 1; // 

        for (int i = 8; i < data.Length; i += 2)
        {
            switch (data.Substring(i, 2))
            {
                case("11"): // dissolving block
                    if (minLvl < 2) minLvl = 2;
                    break;
                case ("12"): // spikeblock
                    if (minLvl < 3) minLvl = 3;
                    break;
                case ("13"): // invis block
                    if (minLvl < 4) minLvl = 4;
                    break;
            }
        }
        if ((setting == "02" || setting == "2") && minLvl < 5) minLvl = 5;
        return minLvl;
    }
}